package com.yx.cloud.activity;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.TextView;

import com.iot.product.sweeper.bean.CameraSettings;
import com.iot.sweeper.AppContext;
import com.iot.sweeper.param.CleanSettingParam;
import com.yx.cloud.R;
import com.yx.cloud.base.BaseActivity;
import com.yx.cloud.databinding.HomeEnvironmentRecognitionActivityBinding;
import com.yx.cloud.utils.Intent_utils;
import com.yx.cloud.utils.RomUtils;
import com.yx.cloud.utils.SharedPrefsUtil;
import com.iot.product.sweeper.bean.Robot;
import com.iot.common.utils.Event;
import com.iot.common.utils.LogUtils;

import org.greenrobot.eventbus.EventBus;import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * 家居环境识别
 */
public class HomeEnvironmentRecognitionActivity extends BaseActivity implements View.OnClickListener {
    private HomeEnvironmentRecognitionActivityBinding binding;
    private Robot robot;
    private CameraSettings cameraSettings;

    @Override
    protected void onCreate() {
        super.onCreate();
        binding = HomeEnvironmentRecognitionActivityBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        if (!EventBus.getDefault().isRegistered(this)) {//加上判断
            EventBus.getDefault().register(this);
        }
        initData();
    }

    private void initData() {
        robot = AppContext.get().getCurrentDevice();
        cameraSettings = robot.getCameraSettings();

        if (cameraSettings.isHomeEnvironmentRecognitionSwitch()) {
            binding.buttonHomeEnvironment.setBackgroundResource(R.mipmap.switch_track_on);
        } else {
            binding.buttonHomeEnvironment.setBackgroundResource(R.mipmap.switch_track_off);
        }
        binding.ivCameraEnviromentReturn.setOnClickListener(this);
        binding.rlHomePolicy.setOnClickListener(this);
        binding.buttonHomeEnvironment.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (RomUtils.isFastClick()) {
            return;
        }
        int id = v.getId();
        if (id == R.id.iv_cameraEnviromentReturn) {
            Intent_utils.backIntent(this);
        } else if (id == R.id.rl_homePolicy) {
            Intent_utils.enterIntentContext(this, VideoPolicyActivity.class, "Policy", "home");
        } else if (id == R.id.button_Home_Environment) {
            boolean homePolicyState = SharedPrefsUtil.getValue(this, "homePolicyState", "home_Polic", false);
            if (!homePolicyState) {
                homeEnvironmentHintDialog(getString(R.string.MV_Video_Env_Privacy_title), getString(R.string.Home_Environment_Policy_hint), "home_policy");
                return;
            }
            if (!cameraSettings.isAiAvoidanceSwitch()) {
                //判断AI避障开关是否打开
                aIAvoidanceHintDialog(getString(R.string.Home_Environment_Recognition_Enabled), getString(R.string.to_turn_on_hint), "ai_switch");
                return;
            }
            if (cameraSettings.isHomeEnvironmentRecognitionSwitch()) {
                aIAvoidanceHintDialog(getString(R.string.Home_Environment_Recognition_Enabled), getString(R.string.to_turn_on_hint), "homeSwitch_close");
                AppContext.getProductService().setDeviceCleanSettings(CleanSettingParam.newBuilder().setHomeEnvironmentRecognitionSwitch(false).build());
            } else {
                AppContext.getProductService().setDeviceCleanSettings(CleanSettingParam.newBuilder().setHomeEnvironmentRecognitionSwitch(true).build());
            }
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(Event event) {
        switch (event.getType()) {
            case Event.MESSAGE_TYPE_CAMERASETTING:
                if (cameraSettings.isHomeEnvironmentRecognitionSwitch()) {
                    binding.buttonHomeEnvironment.setBackgroundResource(R.mipmap.switch_track_on);
                } else {
                    binding.buttonHomeEnvironment.setBackgroundResource(R.mipmap.switch_track_off);
                }
                break;
        }
    }

    private void homeEnvironmentHintDialog(String dialogTitle, String content, String tag) {
        Dialog dialog = new Dialog(HomeEnvironmentRecognitionActivity.this);
        dialog.setContentView(R.layout.disclaimer_dialog);
        dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        dialog.setCancelable(false);
        dialog.setCanceledOnTouchOutside(false);

        TextView mTvTitle = dialog.findViewById(R.id.tv_title);
        TextView mTvContent = dialog.findViewById(R.id.tv_content);
        TextView mTvConsent = dialog.findViewById(R.id.btn_agree);
        TextView mTvReject = dialog.findViewById(R.id.btn_reject);
        TextView mTvDisclaimerServer = dialog.findViewById(R.id.tv_disclaimerServer);
        TextView mTvDisclaimerPrivacy = dialog.findViewById(R.id.tv_disclaimerPrivacy);
        mTvDisclaimerServer.setVisibility(View.GONE);
        mTvTitle.setText(dialogTitle);
        mTvContent.setText(content);
        mTvDisclaimerPrivacy.setText("《" + getString(R.string.MV_Video_Env_Privacy_title) + "》");

        mTvDisclaimerPrivacy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (RomUtils.isFastClick()) {
                    return;
                }
                Intent_utils.enterIntentContext(HomeEnvironmentRecognitionActivity.this, VideoPolicyActivity.class, "Policy", "home");
                dialog.dismiss();
            }
        });

        mTvConsent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (RomUtils.isFastClick()) {
                    return;
                }
                if ("ai".equals(tag)) {
                    AppContext.getProductService().setDeviceCleanSettings(CleanSettingParam.newBuilder().setAirCleanerSwitch(true).build());//Ai开关
                } else {
                    SharedPrefsUtil.putValue(HomeEnvironmentRecognitionActivity.this, "homePolicyState", "home_Polic", true);//家居隐私政策
                    AppContext.getProductService().setDeviceCleanSettings(CleanSettingParam.newBuilder().setHomeEnvironmentRecognitionSwitch(true).build());
                }
                dialog.dismiss();
            }
        });
        mTvReject.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (RomUtils.isFastClick()) {
                    return;
                }
                dialog.dismiss();
            }
        });
        dialog.show();
    }

    Dialog aIAvoidanceDialog;

    @SuppressLint("LongLogTag")
    private void aIAvoidanceHintDialog(String dialogTitle, String content, String tag) {
        aIAvoidanceDialog = new Dialog(HomeEnvironmentRecognitionActivity.this);
        aIAvoidanceDialog.setContentView(R.layout.outlogin_dialog);
        aIAvoidanceDialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        aIAvoidanceDialog.setCanceledOnTouchOutside(false);
        TextView mBtnOutLoginConfirm = aIAvoidanceDialog.findViewById(R.id.btn_outLoginConfirm);
        TextView mBtnOutLoginCancel = aIAvoidanceDialog.findViewById(R.id.btn_outLoginCancel);
        TextView mTvDialogTitle = aIAvoidanceDialog.findViewById(R.id.tv_dialogTitle);
        TextView mTvContent = aIAvoidanceDialog.findViewById(R.id.tv_content);
        mTvDialogTitle.setText(dialogTitle);
        mTvContent.setText(content);
        if ("home_policy".equals(tag)) {
            setStringClick(mTvContent);
        }

        mBtnOutLoginConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                aIAvoidanceDialog.dismiss();
            }
        });
        mBtnOutLoginCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                aIAvoidanceDialog.dismiss();
            }
        });
        aIAvoidanceDialog.show();
    }

    private void setStringClick(TextView tv) {
        int start = 0;
        int end = 0;
        final SpannableStringBuilder style = new SpannableStringBuilder();
        //设置文字
        String str = tv.getText().toString().trim();
        style.append(str);
        //设置部分文字点击事件
        ClickableSpan clickableSpan = new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                Intent_utils.enterIntentContext(HomeEnvironmentRecognitionActivity.this, VideoPolicyActivity.class, "Policy", "home");
                aIAvoidanceDialog.dismiss();
            }
        };
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            if ("《".equals(String.valueOf(c))) {
                start = i;
            } else if ("》".equals(String.valueOf(c))) {
                end = i;
            }
        }
        LogUtils.i("uuuuuuuuu : " + start + " : " + end);
        style.setSpan(clickableSpan, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        tv.setText(style);
        //设置部分文字颜色
        ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(Color.parseColor("#0000FF"));
        style.setSpan(foregroundColorSpan, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        //配置给TextView
        tv.setMovementMethod(LinkMovementMethod.getInstance());
        tv.setText(style);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }

}
